#include "theater.h"
int  main()
{
    int i;
    int tcId;
    Employee* currentTC;
    int ticketSum;
    int temp;
    int temp1;
    /* get curTickclerkno */
    initTheater();
    AcquireLock(ticketClerkWorknoLock);
    temp=getMV(ticketClerkWorkno);
    tcId = temp++;
    setMV(ticketClerkWorkno,temp);
    currentTC=&tc[tcId];
    currentTC->id = tcId;
    Printf("ticket clerk %d is going to work\n",tcId);
    ReleaseLock(ticketClerkWorknoLock);

    while(1)
    {
        AcquireLock(lBuyTickets);
        temp=getMV(currentTC->isBreak);
        if (temp)
        {
            /*if clerk is on break,wait for manager signal*/
            Printf("ticketclerk [%d] is going on break.\n", currentTC->id);
            temp=getMV(breakTicketClerk);
            if (!temp) {
                WaitCV(lBuyTickets, cBreakTicketClerk);
            }
            setMV(breakTicketClerk , 0);
            Printf("ticketclerk [%d] is coming off break.\n", currentTC->id);
             setMV(currentTC->isBreak,false);
        }
        /* if have customer, then set busy and signal the customer, else set free*/
        temp=getMV(currentTC->waitingSize);
        if (temp> 0)
        {
            setMV(currentTC->isBusy,true);
            Printf("TicketClerk [%d] has a line length [%d] and is signaling a customer\n",currentTC->id,temp);
            /* currentTC->waitingSize--; */
            SignalCV(lBuyTickets, currentTC->lineCondition);
        }
        else
        {
            Printf("TicketClerk [%d] has no one in line. I am available for a customer.\n",currentTC->id);
            setMV(currentTC->isBusy,false);
        }

        /*sell ticket:interact with customer*/
        AcquireLock(currentTC->lock);
        /* on service*/
        ReleaseLock(lBuyTickets);
        /* wait customer to signal*/
        WaitCV(currentTC->lock,currentTC->msgCondition);
        if(true==getMV(closeTheater)){
		ReleaseLock(currentTC->lock);
		break;
	}
        /* if break*/
        AcquireLock(lBuyTickets);
        temp=getMV(currentTC->isBreak);
        temp1=getMV(currentTC->isBusy);
        if (currentTC->isBreak && !currentTC->isBusy)
        {
            setMV(currentTC->isBusy,false);
            ReleaseLock(currentTC->lock);
            ReleaseLock(lBuyTickets);
            continue;
        }
        else
        {
            setMV(currentTC->isBusy,true);
        }
        ReleaseLock(lBuyTickets);

        /* get tickets sum*/
        ticketSum =getMV(currentTC->ticketSum);
        /* tell customers amount*/
        setMV(currentTC->amount,ticketSum*PRICE_TICKET);
        Printf("TicketClerk [%d] has an order for [%d] and the cost is [%d].\n",currentTC->id,ticketSum,ticketSum*PRICE_TICKET);
        SignalCV(currentTC->lock,currentTC->msgCondition);
        WaitCV(currentTC->lock,currentTC->msgCondition);
        /* cal amount*/
        AcquireLock(lAmount);
        temp=getMV(currentTC->payment);
        addMVbyValue(ticketClerkAmount[currentTC->id] , temp);
        ReleaseLock(lAmount);
        /* get money, handout the tickets*/
        temp=getMV(currentTC->groupId);
       setMV(ticketReceipt[temp] , ticketSum);
        AcquireLock(lTicketSold);
        addMVbyValue(totalTicketSold, ticketSum);
        ReleaseLock(lTicketSold);
        SignalCV(currentTC->lock,currentTC->msgCondition);
        /* customer leave, get next customer*/
        WaitCV(currentTC->lock,currentTC->msgCondition);
        ReleaseLock(currentTC->lock);
    }

    Exit(0);
}

